require(['loading'], function(loading) {
	
	var brackFlag=0;
	
	//获取url中的id参数
	var params = getUrlParams();
	var goodsId = params.id?params.id:"";
	var addrId = localStorage.getItem('addrId');
	clearStorage("submitOrder-goodsId");
	//应用实例化
	var submitOrder = new Vue({
		el: '#submitOrder',
		data: {
			submitable:true,
			address: [],
			farm: [],
			goods: [],
			contractPrice: 0,
			contractNum: 0,
			contractTotal: 0,
			orderRemark:'',
			showLoading: false,
			apiUrl: wxUrl + '/v3.2/order/preOrder',
			submitUrl: wxUrl + '/v3.2/order/newOrder',
			item: {
				params:{
					token: token,
					userToken: userToken,
					goodsId: goodsId,
					addrId: addrId
				}
			},
			item2: {
				params:{
					token: token,
					userToken: userToken,
					addrId: addrId,
					goodsId: goodsId,
					goodsPrice: 0,
					goodsNum: 0,
					needPrice: 0,
					remark: ''
				}
			}
		},
		ready: function() {
	        this.getOrderData();
	    },
		methods: {
			getOrderData: function() {
				this.$http.get(this.apiUrl, this.item)
					.then(function(response) {
						var result = JSON.parse(response.body);
						this.$set('address', result.data.address);
						this.$set('farm', result.data.farm);
						this.$set('goods', result.data.goods);
						var str = localStorage.getItem('orderMessage');
						if(str == '' || str == null){
							if(this.goods.price != null && this.goods.price != ''){
								this.contractPrice = this.goods.price;
							}
							this.contractNum = this.goods.rangeBegin;
							this.contractTotal = this.goods.price * this.goods.rangeBegin;
							this.contractTotal = this.contractTotal.toFixed(2);
						}
					},function(response) {
						//errorCallback
					});
				//读取缓存订单数据
				var str = localStorage.getItem('orderMessage');
				if(str != '' && str != null){
					var result = JSON.parse(str);
					this.contractPrice = result.contractPrice;
					this.contractNum = result.contractNum;
					this.contractTotal = result.contractTotal;
//					$('#orderRemark').val(result.orderRemark);
					this.orderRemark = result.orderRemark;
				}
			},
			changeInput: function(){
				this.contractTotal = $("#contractPrice").val() * $("#contractNum").val();
				this.contractTotal = this.contractTotal.toFixed(2);
			},
			selectAddr: function(){
				saveStorage(); //本地存储
				window.location.href = self_base_path + "/userCenter/addressManagement/addressManagement.html";
			},
			addRemark: function(){
				brackFlag = brackFlag+1;
				$("#remarkId").animate({"bottom": 0});
			},
			closeAddRemark:function(){
				brackFlag = brackFlag-1;
				$("#remarkId").animate({"bottom": '-100%'});
			},
			submitMark: function(){
				var remark = $("#remarkBZ").val();
				$('#orderRemark').val(remark);
				this.orderRemark = remark;
				this.closeAddRemark();
				if(remark.length > 0){
					$('.sdd_remark').text('已填写');
				}else{
					$('.sdd_remark').text('选填:对本次交易的说明');
				}
			},
			submitOrder: function(){
				if (!this.submitable) {
					return;
				}
				
				if (this.address.id) {
					this.item2.params.addrId = this.address.id;
				} else {
					promptBox("收货地址不能为空哦！");
					return;
				}

				if (isNaN(this.contractPrice)) {
					promptBox("合同单价必须为数字！");
					return;
				} else if (this.contractPrice <= 0 ) {
					promptBox("合同单价必须大于0！");
					return;
				}
				if (isNaN(this.contractNum)) {
					promptBox("合同总量必须为数字！");
					return;
				} else if (this.contractNum <= 0 ) {
					promptBox("合同总量必须大于0！");
					return;
				}
				if (isNaN(this.contractTotal)) {
					promptBox("实付总额必须为数字！");
					return;
				} else if (this.contractTotal <= 0 ) {
					promptBox("实付总额必须大于0！");
					return;
				}
			
				//提交订单
				this.submitable = false;
				this.item2.params.goodsPrice = this.contractPrice;
				this.item2.params.goodsNum = this.contractNum;
				this.item2.params.needPrice = this.contractTotal;
				this.item2.params.remark = this.orderRemark;
				console.log(this.orderRemark);
				this.$http.get(this.submitUrl, this.item2)
					.then(function(response) {
						var result = JSON.parse(response.body);
						if(result.data){
							clearStorage("addrId");
							var orderId = result.data.orderId;
							localStorage.removeItem('orderMessage');
							popBackVHistory();
							window.location.href = self_base_path + "/userCenter/myOrder/orderDetail.html?orderId=" + orderId;
						} else {
							this.submitable = true;
						}
					},function(response) {
						this.submitable = true;
						//errorCallback
					});
			}
		}
	});
	//存储webStorage
	function saveStorage(){
		var data = new Object;
		data.contractPrice = $('#contractPrice').val();
		data.contractNum = $('#contractNum').val();
		data.contractTotal = $('#contractTotal').val();
		data.orderRemark = $('#orderRemark').val();
		var orderStr = JSON.stringify(data);
		localStorage.setItem('orderMessage',orderStr);
		localStorage.setItem('submitOrder-goodsId',goodsId); // 标志从此页面跳出去的
		
	}
	//读取webStorage
	function loadSorage(){
		var str = localStorage.getItem('orderMessage');
		var result = JSON.parse(str);
	}
	//清除webStorage
	function clearStorage(key){
//		localStorage.clear();         //全部清除
		localStorage.removeItem(key); //清除指定key的数据
	}
	//清除输入
	$('.contract input').on('keyup',function(){
		$(this).next().show();
	});
	$('.clear').click(function(){
	    $(this).hide();
	    $(this).prev().val('');
	});
	//防止底部的fixed 被键盘顶上来
	$('#contractTotal,#contractPrice,#contractNum').on('focus',function(){
		$('.sdd_orderBtn').css('position','static');
	}).on('blur',function(){
		$('.sdd_orderBtn').css('position','fixed');
	});
	// 为可控制返回指定页面所有添加pushState
	history.pushState({}, document.title, null);
	pushVHistory();
	window.addEventListener("popstate", function() {
		if(brackFlag<1){
			goBackVHistory();
		}else{
			history.pushState({}, document.title, null);
			brackFlag = brackFlag-1;
			//隐藏掉备注框
			$("#remarkId").animate({"bottom": '-100%'});
		}
	});
	

});
